﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LibraryO2S;

public partial class Administrator_StatisticalWarehousing : System.Web.UI.Page
{
    Dataprocess data = new Dataprocess();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            CollectionPager1.DataSource = LoadData().DefaultView;
            CollectionPager1.PageSize = 15;
            CollectionPager1.BindToControl = rptData;
            rptData.DataSource = CollectionPager1.DataSourcePaged;
            rptData.DataBind();

            txtdate.Text = DateTime.Now.Day.ToString("00") + "/" + DateTime.Now.Month.ToString("00") + "/" + DateTime.Now.Year.ToString("0000");
            txtdate1.Text = DateTime.Now.Day.ToString("00") + "/" + DateTime.Now.Month.ToString("00") + "/" + DateTime.Now.Year.ToString("0000");

            LoadDrop();





            ddlnhomsp.Items.FindByText("").Selected = true;
            ddlvn.Items.FindByText("").Selected = true;
        }
    }

    private void LoadDrop()
    {
        ddlnhomsp.DataSource = data.allSystem("select * from tblProductCategory ");
        ddlnhomsp.DataTextField = "Name";
        ddlnhomsp.DataValueField = "Id";
        ddlnhomsp.DataBind();

        ddlvn.DataSource = data.allSystem("select * from tblUser");
        ddlvn.DataTextField = "FullName";
        ddlvn.DataValueField = "Code";
        ddlvn.DataBind();


        ddlnhomsp.Items.Add("");
        ddlvn.Items.Add("");
    }

    public DataTable LoadData()
    {
        DataTable table = new DataTable();
        DataColumn dtColumn = new DataColumn("date");
        table.Columns.Add(dtColumn);
        DataColumn dtColumn1 = new DataColumn("code");
        table.Columns.Add(dtColumn1);
        DataColumn dtColumn2 = new DataColumn("name");
        table.Columns.Add(dtColumn2);
        DataColumn dtColumn3 = new DataColumn("tennhom");
        table.Columns.Add(dtColumn3);
        DataColumn dtColumn4 = new DataColumn("truocnhap");
        table.Columns.Add(dtColumn4);
        DataColumn dtColumn5 = new DataColumn("slnhap");
        table.Columns.Add(dtColumn5);
        DataColumn dtColumn6 = new DataColumn("tong");
        table.Columns.Add(dtColumn6);


        DataTable dbimp = data.allSystem("select dbo.tblImExport.Date, dbo.tblProduct.Code, dbo.tblProduct.Name, dbo.tblProduct.ProductCategoryID, dbo.tblImExport.BeginQuantity, tblImExport.Quantity, dbo.tblImExport.EndQuantity from  tblImExport LEFT JOIN tblProduct ON dbo.tblImExport.ProductCode=dbo.tblProduct.Code where Action='IMP' order by dbo.tblImExport.Date desc ");
        foreach (DataRow dr in dbimp.Rows)
        {
            string namecate = "";
            int cateproduct = 0;

            string idcate = dr["ProductCategoryID"].ToString();

            if ("NULL".Equals(idcate))
            {

                cateproduct += 1;

            }
            else
            {
                DataTable dbcateproduct = data.allSystem("select * from tblProductCategory where Id='" + idcate + "' ");
                foreach (DataRow drcate in dbcateproduct.Rows)
                {
                    namecate = drcate["Name"].ToString();
                }
            }

            DataRow dtRow = table.NewRow();
            dtRow[0] = dr["Date"].ToString();
            dtRow[1] = dr["Code"].ToString();
            dtRow[2] = dr["Name"].ToString();
            dtRow[3] = namecate;
            dtRow[4] = dr["BeginQuantity"].ToString();
            dtRow[5] = dr["Quantity"].ToString();
            dtRow[6] = dr["EndQuantity"].ToString();
            table.Rows.Add(dtRow);
        }
        return table;
    }
    public DataTable LoadDataByDate(string date, string date1, string cateproid, string usercode)
    {

        DataTable table = new DataTable();
        DataColumn dtColumn = new DataColumn("date");
        table.Columns.Add(dtColumn);
        DataColumn dtColumn1 = new DataColumn("code");
        table.Columns.Add(dtColumn1);
        DataColumn dtColumn2 = new DataColumn("name");
        table.Columns.Add(dtColumn2);
        DataColumn dtColumn3 = new DataColumn("tennhom");
        table.Columns.Add(dtColumn3);
        DataColumn dtColumn4 = new DataColumn("truocnhap");
        table.Columns.Add(dtColumn4);
        DataColumn dtColumn5 = new DataColumn("slnhap");
        table.Columns.Add(dtColumn5);
        DataColumn dtColumn6 = new DataColumn("tong");
        table.Columns.Add(dtColumn6);



        DataTable dbimp = data.allSystem("select dbo.tblImExport.Date, dbo.tblProduct.Code, dbo.tblProduct.Name, dbo.tblProduct.ProductCategoryID, dbo.tblImExport.BeginQuantity, tblImExport.Quantity, dbo.tblImExport.EndQuantity from  tblImExport LEFT JOIN tblProduct ON dbo.tblImExport.ProductCode=dbo.tblProduct.Code where Action='IMP' and dbo.tblImExport.Date>='" + date + "' and dbo.tblImExport.Date<='" + date1 + "' and dbo.tblProduct.ProductCategoryID like '%" + cateproid + "%' and  dbo.tblImExport.UserCode like '%" + usercode + "%'  order by dbo.tblImExport.Date desc  ");
        foreach (DataRow dr in dbimp.Rows)
        {
            string namecate = "";
            int cateproduct = 0;

            string idcate = dr["ProductCategoryID"].ToString();

            if ("NULL".Equals(idcate))
            {

                cateproduct += 1;

            }
            else
            {
                DataTable dbcateproduct = data.allSystem("select * from tblProductCategory where Id='" + idcate + "' ");
                foreach (DataRow drcate in dbcateproduct.Rows)
                {
                    namecate = drcate["Name"].ToString();
                }
            }


            DataRow dtRow = table.NewRow();
            dtRow[0] = dr["Date"].ToString();
            dtRow[1] = dr["Code"].ToString();
            dtRow[2] = dr["Name"].ToString();
            dtRow[3] = namecate;
            dtRow[4] = dr["BeginQuantity"].ToString();
            dtRow[5] = dr["Quantity"].ToString();
            dtRow[6] = dr["EndQuantity"].ToString();
            table.Rows.Add(dtRow);
        }
        return table;

    }
    protected void btnAdnew1_Click(object sender, EventArgs e)
    {
        Response.Redirect("BannerAdd.aspx");
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {

    }
    protected void btnUpdateDisplayOrder_Click(object sender, EventArgs e)
    {
        rptData.DataSource = LoadData();
        rptData.DataBind();
    }
    protected void btnthongke_Click(object sender, EventArgs e)
    {


        string date = txtdate.Text.Substring(3, 2) + "/" + txtdate.Text.Substring(0, 2) + "/" + txtdate.Text.Substring(6, 4) + " 00:00";
        string date1 = txtdate1.Text.Substring(3, 2) + "/" + txtdate1.Text.Substring(0, 2) + "/" + txtdate1.Text.Substring(6, 4) + " 23:59";
        string catepro = ddlnhomsp.SelectedValue;
        string usercode = ddlvn.SelectedValue;


        CollectionPager1.DataSource = LoadDataByDate(date, date1, catepro, usercode).DefaultView;
        CollectionPager1.PageSize = 15;
        CollectionPager1.BindToControl = rptData;
        rptData.DataSource = CollectionPager1.DataSourcePaged;
        rptData.DataBind();

    }
}